package com.zhuoge.homework;

public class ExceptionTest1 {
    public static void main(String[] args) {
        try {
            Person person = new Person("张三",-0.5);
        }catch (Exception a){
            a.printStackTrace();
        }

        try {
            Person person2 = new Person();
            person2.setLifeValue(10.0);
            person2.setLifeValue(-10.0);
        }catch (Exception a){
            a.printStackTrace();
        }
    }
}
class NoLifeValueException extends RuntimeException{
    public NoLifeValueException() {
    }

    public NoLifeValueException(String message) {
        super(message);
    }
}
class Person{
    private String name;
    private double lifeValue;

    public Person() {
    }

    public Person(String name, double lifeValue) {
        this.name = name;
        this.lifeValue = lifeValue;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getLifeValue() {
        return lifeValue;
    }

    public void setLifeValue(double lifeValue) {
        this.lifeValue = lifeValue;
        if (lifeValue < 0){
            throw new NoLifeValueException("生命值不能为负数："+getLifeValue());
        }
    }
}